package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bocai.yuweiwangluo.adapter.FootballTalkListAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BBSListBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IBBSListView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class BBSPresenter
{
    private BBSListBean bbsListBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IBBSListView ibbsListView;
    private Context context;
    private BaseView baseView;
    private  FootballTalkListAdapter footballTalkListAdapter;
    public BBSPresenter(Context context, IBBSListView ibbsListView, BaseView baseView)
    {
        this.context = context;
        this.ibbsListView = ibbsListView;
        this.baseView=baseView;
    }

    /**
     * 获取讨论列表
     */
    public void getBBSList(String type_id)
    {
        baseView.showLoading();
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getbbsList(UrlData.BASEURL + UrlData.BBSTYPELIST, type_id,ibbsListView.getmyPage() + "", handler);
    }
    public BBSListBean getData(){

        return bbsListBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);

            String result = (String) msg.obj;
            Gson gson = new Gson();
            ibbsListView.onLoad();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        baseView.hideLoading();
                        Log.e("loginresult", result);
                        bbsListBean= gson.fromJson(result, BBSListBean.class);
                        if(bbsListBean.getData().size()==0&ibbsListView.getmyPage()==1){
                            ibbsListView.getnoContent().setVisibility(View.VISIBLE);
                            ibbsListView.getListView().setVisibility(View.GONE);
                        }else {
                            ibbsListView.getnoContent().setVisibility(View.GONE);
                            ibbsListView.getListView().setVisibility(View.VISIBLE);
                            if(ibbsListView.getmyPage()==1){
                                footballTalkListAdapter=new FootballTalkListAdapter(context,bbsListBean);
                                ibbsListView.getListView().setAdapter(footballTalkListAdapter);
                            }else{
                                footballTalkListAdapter.addData(bbsListBean);
                            }
                            if(bbsListBean.getTotal()<=(bbsListBean.getEach()*ibbsListView.getmyPage()))
                            {
                                ibbsListView.getListView().setPullLoadEnable(false);
                            }
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.hideLoading();
                        baseView.showError(result);
                        ibbsListView.getllRefresh().setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}